#include <func.h>
#include "head.h"

int makeToken(char* salt, char* usr_name, char* token){
    //"盐值 + 用户名 + 登陆时间"
    time_t t;
    struct tm *timeinfo;
    time(&t);
    timeinfo = localtime(&t);
    int year = timeinfo->tm_year + 1900;
    int month = timeinfo->tm_mon + 1;
    int day = timeinfo->tm_mday;
    int hour = timeinfo->tm_hour;
    // int min = timeinfo->tm_min;
    // int sec = timeinfo->tm_sec;
    char usrInfo[100] = {0};
    sprintf(usrInfo, "%s%d%d%d%d", usr_name, year, month, day, hour);
    //puts(usrInfo);
    char usrInfoWithSalt[100] = {0};
    sprintf(usrInfoWithSalt, "%s%s", salt, usrInfo);
    //puts(usrInfoWithSalt);

    //先获取md5码
    unsigned char Md5[16];
    char md5[100] = {0};
    char *q = md5;
    MD5(usrInfoWithSalt, strlen(usrInfoWithSalt), Md5);
    for(int i = 0; i < 16; i++){
        sprintf(q, "%02x", Md5[i]);
        q += 2;
    }
    //puts(md5);
    //printf("md5length = %ld\n", strlen(md5));
    strcpy(token, md5);
    return 0;
}